﻿package net.wangit.view.chart;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.wangit.Unit;
import net.wangit.User;
import net.wangit.context.ContextUtil;
import net.wangit.framework.session.AgileSession;
import net.wangit.user.Developer;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.unit.UnitFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.chart.ChartUtilities;


public class ChartServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory
			.getLog(net.wangit.view.chart.ChartServlet.class);
	private static final String CONTENT_TYPE = "image/png";

	public ChartServlet() {
	}

	public void init() throws ServletException {
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		OutputStream out;
		String domain;
		String chartId;
		ContextUtil.createContext(request, response, getServletConfig());
		out = null;
		response.setContentType(ChartServlet.CONTENT_TYPE);
		domain = request.getParameter("domain");
		chartId = request.getParameter("chartid");
		AgileSession session = (AgileSession) request.getSession()
				.getAttribute("net.wangit.framework.session.AgileSession");
		if (session == null) {
			out = response.getOutputStream();
			setMessage(out, "illegal access!");
			return;
		}
		User user = session.getVisitor().getUser();
		if (!Checker.isEmpty(chartId)) {
			if (!(user instanceof Developer)) {
				out = response.getOutputStream();
				setMessage(out, "can't use 'chartid' parameter.");
				return;
			}
		} else {
			String unitId = request.getParameter("uid");
			String elementId = request.getParameter("eid");
			Unit unit = UnitFactory.getUnitWithoutClone(domain, unitId);
			ElementComponent element = (ElementComponent) unit
					.getElement(elementId);
			if (element.hasAccessPrivilege()
					&& !user.hasElementPrivilege(element)) {
				out = response.getOutputStream();
				setMessage(out, "您没有得到授权.");
				return;
			}
			chartId = (String) element.getObject();
		}
		try {
			out = response.getOutputStream();
			Chart chart = ChartProvider.getChart(domain, chartId);
			org.jfree.chart.JFreeChart jFreeChart = chart.getJFreeChart();
			ChartUtilities.writeChartAsPNG(out, jFreeChart, chart.getWidth(),
					chart.getHeight());
		} catch (Exception e) {
			setMessage(out, "无法输出统计图.");
			log.error("chart exception", e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	private void setMessage(OutputStream out, String message) {
		try {
			BufferedImage image = new BufferedImage(200, 200, 1);
			Graphics g = image.getGraphics();
			g.setColor(Color.white);
			g.fillRect(1, 1, 198, 198);
			g.setColor(Color.black);
			g.drawString(message, 50, 50);
			ChartUtilities.writeBufferedImageAsPNG(out, image);
			out.write(message.getBytes());
		} catch (Exception e) {
			log.error(e);
		}
	}

}
